package com.example.auth3.controller;

import com.example.auth3.service.PrivilegeUserService;
import com.example.util.model.PrivilegeUser;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * controller 配置
 * @author shangSshang
 */
@Slf4j
//@RestController
@Controller
//@RequestMapping("/")
public class IndexController {
    private static final Logger LOG = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private PrivilegeUserService privilegeUserService;
//    @PreAuthorize("hasRole('SA')")
    @RequestMapping(value = "/index",method = RequestMethod.GET)
//    @ResponseBody
    public  String index(ModelMap map, HttpServletRequest request, Principal principal){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LOG.info("登录成功!");
        return "index";
    }

    /**
     * 使用配置文件中的自定义常量
     */
    @Value("${myapp.client_name}")
    private String clientName;
    @Value("${myapp.client_pwd}")
    private String clientAuth;
    @Value("${myapp.grant_type}")
    private String grantType;
    @Value("${myapp.scope}")
    private String scope;
//    @RequestMapping(value = "/login1")
    @GetMapping(value = "/login")
    public  String login(ModelMap map, Principal principal){
//        return "hello ，登录成功！";
//        PrivilegeUser user = privilegeUserService.selectByPrimaryKey("admin");
//        map.addAttribute("user",user);
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LOG.info("登录时界面");
        map.addAttribute("clientName",clientName);
        map.addAttribute("scope",scope);
        map.addAttribute("grantType",grantType);
        map.addAttribute("clientAuth",clientAuth);
//        LOG.info("authentication : "+authentication.toString());
        return "login";
    }


    @GetMapping(value = "/login/index")
    @ResponseBody
    public  String inIndex(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return "hello inIndex !";
    }


}
